home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / split.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  83 lines

  1. /***********************************************************************
  2.  * $Id: split.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *    Try to split a filename into directory and file components
  8.  *    return -1 on error
  9.  ***********************************************************************/
  10. #if !defined(lint) && defined(F_ID)
  11. char *id_split = "$Id: split.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "unistd.h"
  17. #include "ulib.h"
  18.  
  19. #define DO_STAT
  20.  
  21. #ifdef DO_STAT
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. static struct stat buf;
  25. #endif
  26.  
  27.  
  28. int
  29. split_fname(char dir[], char f[], const char *comb)
  30. {
  31.     char *q;
  32.     char qq[PATH_MAX + 2];
  33.  
  34.     /* expand dirname ~, ../ ./ etc to abs. path */
  35.     strcpy(qq, comb);
  36.     (void) fix_dirname(qq);
  37.  
  38. #ifdef DO_STAT
  39.     if (stat(qq, &buf))
  40.       {
  41.       perror(qq);
  42.       return -1;
  43.       }
  44.  
  45.     if ((buf.st_mode & S_IFDIR) == S_IFDIR)
  46.       {
  47.       strcpy(dir, qq);
  48.       f[0] = '\0';
  49.       return 0;
  50.       }
  51.  
  52.     if ((buf.st_mode & S_IFREG) != S_IFREG)
  53.     return -1;
  54. #endif
  55.  
  56.     if ((q = strrchr(qq, '/')) != 0)
  57.       {
  58.       strcpy(f, q + 1);
  59.       strncpy(dir, qq, q - qq);
  60.       dir[q - qq] = '\0';
  61.       }
  62.     else
  63.     return -1;
  64.     return 0;
  65. }
  66.  
  67. #ifdef TEST
  68.  
  69. main()
  70. {
  71.     char ll[1250], dir[1024], f[1024];
  72.  
  73.     while (gets(ll))
  74.       {
  75.       if (split_fname(dir, f, ll) == 0)
  76.           fprintf(stderr, "dir=%s\t f=%s ext=%s\n", dir, f, getext(f));
  77.       else
  78.           fprintf(stderr, "bad stuff\n");
  79.       }
  80. }
  81.  
  82. #endif
  83.